<template>
  <el-card class="box-card card-main" shadow="never">
    <div slot="header">
      <el-form
        :inline="true"
        :model="serchData"
        class="demo-form-inline"
        label-width="90px"
        label-position="right"
        size="mini"
      >
        <el-form-item label="用户：">
          <el-input v-model="serchData.nickname" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="serchDate"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchList">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" :data="data" border stripe size="small" style="width: auto">
      <el-table-column type="selection" align="left" width="35" />
      <el-table-column width="100" align="left" label="日志ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column width="150" align="left" label="用户">
        <template slot-scope="scope">{{ scope.row.user.nickname }}</template>
      </el-table-column>
      <el-table-column width="200" align="left" label="动作">
        <template slot-scope="scope">{{ scope.row.module }}</template>
      </el-table-column>
      <el-table-column width="100" align="left" label="状态">
        <template slot-scope="scope">{{ scope.row.flag === true ? '成功' : '失败' }}</template>
      </el-table-column>
      <el-table-column align="left" label="备注">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column width="150" align="left" label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column width="100" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.username !== 'admin'"
            type="danger"
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="deleteRow(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="pageInfo.pageNum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.pageCount"
      @size-change="changePageSize"
      @current-change="changePageNum"
    />
  </el-card>
</template>

<script>
import { requestListPage } from '@/api/log'
export default {
  data() {
    return {
      loading: false,
      serchData: { nickname: '', beginTime: '', endTime: '' },
      serchDate: [],
      pageInfo: { pageNum: 1, pageSize: 10, pageCount: 0 },
      data: []
    }
  },
  computed: {
    serchInfo: function() {
      const pageInfo = this.pageInfo
      const serchData = this.serchData
      serchData.beginTime = this.serchDate[0]
      serchData.endTime = this.serchDate[1]
      pageInfo.serchData = serchData
      return pageInfo
    }
  },
  mounted() {
    this.getUserPageList()
  },
  methods: {
    resetSearch() {
      // 重置搜索条件方法
      this.serchData = {}
      this.serchDate = []
    },
    searchList() {
      this.getUserPageList()
    },
    changePageSize(data) {
      // console.log(data)
      this.pageInfo.pageSize = data
      this.getUserPageList()
    },
    changePageNum(data) {
      this.pageInfo.pageNum = data
      this.getUserPageList()
    },
    // 获取分页用户列表
    getUserPageList() {
      this.loading = true
      requestListPage(this.serchInfo).then((result) => {
        this.data = result.list || {}
        this.pageInfo.pageCount = result.total
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
</style>
